package com.soso.taste.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.soso.taste.pojo.Amass;
import com.soso.taste.pojo.Taste;
import com.soso.taste.service.TasteServiceImpl;
import com.soso.taste.util.Splitter;

import static com.soso.taste.util.Decode.*;

public class TasterAction implements ServletRequestAware,ServletResponseAware{
	
	@SuppressWarnings("unused")
	private HttpServletRequest request;
	@SuppressWarnings("unused")
	private HttpServletResponse response;
	private String call;
	private String openid;
	private String part;
	private String title;
	private String href;
	
	public String execute() throws IOException {
		//PrintWriter out = response.getWriter();

		part = decodeURI(part);
		title = decodeURI(title);
		Taste taste = new Taste(call, openid, part, title, href);
		TasteServiceImpl ts = TasteServiceImpl.getInstance();
		if(title.length()<=2)return null;
		if(ts.IsExist(openid, href)==0){
			ts.Save(taste);
			//System.out.println("title="+title);
			List<String> words = Splitter.statistic(title, 3);
			List<Amass> amasses = new ArrayList<Amass>();
			for(int i=0;i<words.size();i++){
				//System.out.println(words.get(i));
				amasses.add(new Amass(openid, words.get(i), title, href));
			}
			ts.Save(amasses);
 		}
		return null;
	}
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getCall() {
		return call;
	}

	public void setCall(String call) {
		this.call = call;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public String getPart() {
		return part;
	}

	public void setPart(String part) {
		this.part = part;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}
	
}
